import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import moment from 'moment';
import {Row,Col,Card,Form,Input,Select,Icon,Button,Menu,InputNumber,Modal,message,Badge,Divider,Radio,label,Table,} from 'antd';
import StandardTable from '../../components/StandardTable';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import styles from '../Mana/Role.less';


const Option = Select.Option;
const { TextArea } = Input;
const Search = Input.Search;
const RadioGroup = Radio.Group;
const FormItem = Form.Item;
const getValue = obj =>Object.keys(obj).map(key => obj[key]).join(',');



const CreateForm = Form.create()(props => {
  const { modalVisible, form, handleAdd, handleModalVisible,title,obj,successful} = props;
  const okHandle = () => {
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      	form.resetFields();
	    handleAdd(title,obj.userId,fieldsValue);
    });
  };
 const {getFieldDecorator } = form;
  return (
    <Modal
      maskClosable={false}
      destroyOnClose={true}
     title={title}
      visible={modalVisible}
      onOk={okHandle}
      onCancel={() => handleModalVisible(false,'','')}
    >
   		<Form layout="horizontal">
      		<FormItem label="推广人数"  labelCol={{span: 8}}  wrapperCol={{span: 12}} className={styles.w50left}>
	            {getFieldDecorator('thresholdNumber', {
	            	initialValue:obj.thresholdNumber,
	              rules: [{ required: true, message: '推广人数不能为空!' }],
	            })(
	              <InputNumber min={1} max={10000000}  placeholder="请输入"  style={{width:'100%',marginLeft:'2px'}}/>
	            )}
          	</FormItem>
          	<FormItem label="返利金额"  labelCol={{span: 8}}  wrapperCol={{span: 12}} className={styles.w50left}>
	            {getFieldDecorator('thresholdMoney', {
	            	initialValue:obj.thresholdMoney,
	              rules: [{ required: true, message: '返利金额不能为空!' }],
	            })(
	            <InputNumber min={1} max={10000000}  placeholder="请输入" style={{width:'100%',paddingRight:'3px'}}/>
	        )}
          	</FormItem>
    	</Form>
  </Modal>
  );
});



@connect(({ campus, loading }) => ({
  campus,
  loading: loading.models.campus,
}))
@Form.create()
export default class TableList extends PureComponent {
  state = {
    modalVisible: false,
    expandForm: false,
    selectedRows: [],
    formValues: {},
    obj: "",
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
       type: 'campus/findSale',
       payload: {type:0}
    });
  }
   handleStandardTableChange = (pagination, filtersArg, sorter) => {
    
  };


  handleSelectRows = rows => {
    this.setState({
      selectedRows: rows,
    });
  };

  handleSearch = e => {
	e.preventDefault();
	const {
		dispatch,
		form
	} = this.props;
	form.validateFields((err, fieldsValue) => {
		if(err) return;
		const values = { ...fieldsValue,
		};
		this.setState({
			formValues: values,
		});
		if(values.operatorName == undefined) values.operatorName = "";
		
		dispatch({
			type: 'campus/findSale',
			payload: { ...values
			}
		});
	});
};
  /*重置*/
	handleFormReset = () => {
		const {	form,	dispatch} = this.props;
		form.resetFields();
		this.setState({	formValues: {},});
		dispatch({type: 'campus/findSale',payload: {type:0}});
	}
	/*查询*/
	handleSearch = e => {
	    e.preventDefault();
	    const { dispatch, form } = this.props;
	    form.validateFields((err, fieldsValue) => {
		    if (err) return;
		    const values = {  ...fieldsValue,};
		    this.setState({ formValues: values,});
        dispatch({
	        type: 'campus/findSale',
	        payload:{...values},
        });
	    });
	};
  handleModalVisible = (flag,record,title) => {
  	if(flag==false){
  		this.setState({
	      modalVisible: flag,
	    });
  	}else{
  		var obj="";
	  	if(title!="新建"){
	  		if(record.icon!=undefined){
				
			}
	  		record.state+='';
	  		record.hortationType+='';
	  		obj=record;
	  	}
	    this.setState({
	      modalVisible: !!flag,
	      title: title,
	      obj: obj,
	    });
  	}
  
  };

	renderSimpleForm() {
		    const { getFieldDecorator } = this.props.form;
		    return (
		      <Form onSubmit={this.handleSearch} layout="inline">
		        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
		          <Col md={8} sm={24}>
		            <FormItem label="人员类型">
		              {getFieldDecorator('type')(
		                <Select placeholder="请选择" style={{ width: '100%' }}>
		                  <Option value="0">销售人员</Option>
		                </Select>
		              )}
		            </FormItem>
		          </Col>
		          <Col md={8} sm={24}>
		            <span className={styles.submitButtons}>
		              <Button type="primary" htmlType="submit">
		                查询
		              </Button>
		              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
		                重置
		              </Button>
		            </span>
		          </Col>
		        </Row>
		      	
		      </Form>
		    );
		  }

	
	
  render() {
    const { campus: { data }, loading } = this.props;
    const { selectedRows, modalVisible,title,obj } = this.state;
    const columns = [
    	{
		title: '序号',
		dataIndex: 'referee_id',
		key: 'referee_id',
		render: (text, record, index) => { return index + 1 }
		}, 
		{
		title: '推荐人姓名',
		dataIndex: 'referee_name',
		}, 
		{
		title: '推荐人电话',
		dataIndex: 'referee_phone',
		},
		{
		title: '被推荐人姓名',
		dataIndex: 'referrals_name',
		},
		{
		title: '被推荐人电话',
		dataIndex: 'referrals_phone',
		},
		{
		title: '推荐金额',
		dataIndex: 'recommend_money',
		},
		{
		title: '是否提现',
		dataIndex: 'is_withdraw',
		render: (text, record, index) => {
				if(record.is_withdraw == 0) {
					return "未获得推荐"
				} else if(record.is_withdraw == 1) {
					return "已获得推荐金额"
				}else if(record.is_withdraw == 2) {
					return "提现失败"
				}else if(record.is_withdraw == 3) {
					return "提现成功"
				}else if(record.is_withdraw == 4) {
					return "审核中"
				}
			}
		},
		{
		title: '人员类型',
		dataIndex: 'type',
		},
	 ];
    const parentMethods = {
      handleAdd: this.handleAdd,
      successful: this.successful,
     	handleModalVisible: this.handleModalVisible,
      confirmModel:this.confirmModel,
    };

    return (
    	<PageHeaderLayout title="前台系统/销售推广">
      <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListForm}>{this.renderSimpleForm()}</div>
            <StandardTable
              selectedRows={selectedRows}
              loading={loading}
              data={data}
              columns={columns}
              onSelectRow={this.handleSelectRows}
              onChange={this.handleStandardTableChange}
            />
          </div>
        <CreateForm {...parentMethods} modalVisible={modalVisible} title={title} obj={obj}/>
      </Card>
      </PageHeaderLayout>
    );
  }
}
